<?php
require_once("includes/pagetop.php");

if($_FILES["selectedFile"]!="")
{
	if(isset($_SESSION['Allyfolio_User_ID']) && $_SESSION['Allyfolio_User_ID']!="")
	{
		if(isset($_REQUEST["Media_Type"]) && $_REQUEST["Media_Type"]!="")
	    {
	    	$User_ID=$_SESSION['Allyfolio_User_ID'];
	    	$Privacy_Type_ID=1;
	    	$Post_Title=$_REQUEST["Album_Name"];
	    	$PostedContent="";
	    	$Post_Type_ID=8;
	    	if($_REQUEST["Media_Type"]=="VIDEO")
	    	{
	    		$Post_Type_ID=9;
	    	}	
	    	$Parent_ID=0;
	    	$Source_ID=0;
	    	$Friend_IDs="";
	    	$allowed_friends="";
	    	$ignore_friends="";
	    	$Posted_On_Date=strtotime(date("Y-m-d H:i:s", time()));
	    	$Last_Modified_Date=$Posted_On_Date;
	    	$Status="Active";
	    	
	    	$qryInsertPost="insert into af_user_posts_activities set User_ID=:User_ID, Privacy_Type_ID=:Privacy_Type_ID,Post_Title=:Post_Title, PostedContent=:PostedContent,  	Post_Type_ID=$Post_Type_ID,Parent_ID=:Parent_ID,Source_ID=:Source_ID,Friend_IDs=:Friend_IDs,allowed_friends=:allowed_friends,ignore_friends=:ignore_friends,Posted_On_Date=:Posted_On_Date,Last_Modified_Date=:Last_Modified_Date,Status=:Status ";
			$resInsertPost=$pdodbobj->query($qryInsertPost,array(":User_ID"=>$User_ID,":Privacy_Type_ID"=>$Privacy_Type_ID,":Post_Title"=>$Post_Title,":PostedContent"=>$PostedContent,":Parent_ID"=>$Parent_ID,":Source_ID"=>$Source_ID,":Friend_IDs"=>$Friend_IDs,":allowed_friends"=>$allowed_friends,":ignore_friends"=>$ignore_friends,":Posted_On_Date"=>$Posted_On_Date,":Last_Modified_Date"=>$Last_Modified_Date,":Status"=>$Status ));
			
			$lastInsertedID= $pdodbobj->last_id();
	    	
			if($lastInsertedID>0)
			{
				for($i=0; $i<count($_FILES['selectedFile']['name']); $i++)
				{
					$ext = explode('.', basename( $_FILES['selectedFile']['name'][$i]));
					if(in_array(strtolower($ext[count($ext)-1]),array("jpg","jpeg","png","bmp","gif","vob","mp4","3gp")))
					{
						$target_path = 'uploads/album_media/'.$User_ID;
						if(!file_exists($target_path))
				        {
							mkdir($target_path,0777);
						}
						$fileName=$_FILES['selectedFile']['name'][$i];
						$Fullpath=$target_path ."/".$fileName;
						
						if(move_uploaded_file($_FILES['selectedFile']['tmp_name'][$i], $Fullpath)) 
						{
							if(in_array(strtolower($ext[count($ext)-1]),array("jpg","jpeg","png","bmp","gif")))
							{
								$mediaType="IMAGE";
							}
							else if(in_array(strtolower($ext[count($ext)-1]),array("vob","mp4","3gp")))
							{
								$mediaType="VIDEO";
							}
							
							//Save in DB
							$qryInsert="insert into af_user_posts_media_uploads set Post_ID=:Post_ID, Media_Path=:Media_Path, Media_Type=:Media_Type,updated_date='".time()."',Status=1";
							$pdodbobj->query($qryInsert,array(":Post_ID"=>$lastInsertedID,":Media_Path"=>$Fullpath,":Media_Type"=>$mediaType ));
							//echo "SUCCESS|~|".$Fullpath."|~|".$mediaType;
							$msg_success="Album has been created successfully";
						}
						else
						{
							//echo "ERROR|~|Looks like there is some problem. Please try after some time..";
							$msg_error="Looks like there is some problem. Please try after some time.";
						}
					}
					else
					{
						//echo "ERROR|~|Please Upload image or video only.";
						$msg_error="Please Upload image or video only.";
					}
				}
			}
			else
			{
				//echo "ERROR|~|Looks like there is some problem. Please try after some time...";
				$msg_error="Looks like there is some problem. Please try after some time.";
			}
		}
	}
}
/*else if($_REQUEST["purpose"]!="" && $_REQUEST["purpose"]=="delete" && $_REQUEST["mediaID"]!="" && $_REQUEST["mediaID"]>0 && $_REQUEST["page"]!=""  )
{
	$pageID=$_REQUEST["page"];
	$qryPageDetail="select cpm.Comp_Page_ID from af_company_page_media as cpm, af_company_page as cp where cpm.Comp_Page_ID= cp.Comp_Page_ID and cp.User_ID =:User_ID and cpm.Media_Upload_ID=:Media_Upload_ID and cp.Page_Slug=:Page_Slug";
	$arrPageDetail=$pdodbobj->fetch_array($pdodbobj->query($qryPageDetail,array(":Media_Upload_ID"=>$_REQUEST["mediaID"],":User_ID"=>$_SESSION["Allyfolio_User_ID"],":Page_Slug"=>$pageID)));

	if(is_array($arrPageDetail) && count($arrPageDetail)>0)
	{
		$qryDelete="update af_company_page_media set Status=2, delete_date='".time()."' where Media_Upload_ID=:Media_Upload_ID";
		$pdodbobj->query($qryDelete,array(":Media_Upload_ID"=>$_REQUEST["mediaID"] ));
		echo "SUCCESS|~|Page media has been deleted successfully.";
	}
	else
	{
		echo "ERROR|~|Looks like there is some problem. Please try after some time.";
	}
}*/
else if($_REQUEST["purpose"]="deletepost" && $_REQUEST["postid"]>0)
{
	if(isset($_SESSION['Allyfolio_User_ID']) && $_SESSION['Allyfolio_User_ID']!="")
	{
		$qryPostDetail="select * from af_user_posts_activities where User_ID =:User_ID and Post_ID=:Post_ID ";
		$arrPostDetail=$pdodbobj->fetch_array($pdodbobj->query($qryPostDetail,array(":User_ID"=>$_SESSION["Allyfolio_User_ID"],":Post_ID"=>$_REQUEST["postid"])));
		if(is_array($arrPostDetail) && count($arrPostDetail)>0)
		{
			$qryDelete="delete from af_user_posts_activities where Post_ID=:Post_ID";
			$pdodbobj->query($qryDelete,array(":Post_ID"=>$_REQUEST["postid"] ));
			echo "SUCCESS|~|Album has been deleted successfully.";			
		}
		else
		{
			echo "ERROR|~|Looks like there is some problem. Please try after some time.";
		}
	}
	else
	{
		echo "ERROR|~|Looks like there is some problem. Please try after some time.";
	}
}
else if($_REQUEST["purpose"]="deletemedia" && $_REQUEST["postMediaID"]>0)
{
	if(isset($_SESSION['Allyfolio_User_ID']) && $_SESSION['Allyfolio_User_ID']!="")
	{
		$qryPostDetail="select upmu.* from af_user_posts_activities as upa left join af_user_posts_media_uploads as upmu on upa.Post_ID=upmu.Post_ID where User_ID=:User_ID and upmu.Media_Upload_ID=:Media_Upload_ID";
		$arrPostDetail=$pdodbobj->fetch_array($pdodbobj->query($qryPostDetail,array(":User_ID"=>$_SESSION["Allyfolio_User_ID"],":Media_Upload_ID"=>$_REQUEST["postMediaID"])));
		if(is_array($arrPostDetail) && count($arrPostDetail)>0)
		{
			$qryDelete="delete from af_user_posts_media_uploads where Media_Upload_ID=:Media_Upload_ID";
			$pdodbobj->query($qryDelete,array(":Media_Upload_ID"=>$_REQUEST["postMediaID"] ));
			echo "SUCCESS|~|Media has been deleted successfully.";			
		}
		else
		{
			echo "ERROR|~|Looks like there is some problem. Please try after some time.";
		}
	}
	else
	{
		echo "ERROR|~|Looks like there is some problem. Please try after some time.";
	}
}
else if($_REQUEST["purpose"]="deleteblog" && $_REQUEST["blogID"]>0)
{
	if(isset($_SESSION['Allyfolio_User_ID']) && $_SESSION['Allyfolio_User_ID']!="")
	{
		$qryPostDetail="select * from af_blogs where af_blogs.user_id =:user_id and af_blogs.blog_id=:blog_id";
		$arrPostDetail=$pdodbobj->fetch_array($pdodbobj->query($qryPostDetail,array(":user_id"=>$_SESSION["Allyfolio_User_ID"],":blog_id"=>$_REQUEST["blogID"])));
		if(is_array($arrPostDetail) && count($arrPostDetail)>0)
		{
			$qryDelete="delete from af_blogs where af_blogs.user_id =:user_id and af_blogs.blog_id=:blog_id";
			$pdodbobj->query($qryDelete,array(":user_id"=>$_SESSION["Allyfolio_User_ID"],":blog_id"=>$_REQUEST["blogID"]));
			echo "SUCCESS|~|Blog has been deleted successfully.";			
		}
		else
		{
			echo "ERROR|~|Looks like there is some problem. Please try after some time.";
		}
	}
	else
	{
		echo "ERROR|~|Looks like there is some problem. Please try after some time.";
	}
}
?>